//
//  SMHomeViewController.m
//  SaveMoney
//
//  Created by user on 2018/11/21.
//  Copyright © 2018年 Sunflower. All rights reserved.
//

#import "SMHomeViewController.h"

#import "SMMoneyHomeViewController.h"
#import "XWInteractiveTransition.h"
#import "SMAddSaveMoneyTableViewController.h"
#import "AppDelegate.h"
#import "SMMoneyHomeViewController.h"
#import "SMLeftViewController.h"
#import "MSNavigationViewController.h"
#import "SMNULLProjectViewController.h"

#define isiOS10 ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=10)

@interface SMHomeViewController ()<XWPresentedOneControllerDelegate,UINavigationControllerDelegate>{
    id newMessageObserver;//定义全局observer
    id newSaveObserver;//定义全局observer
}
@property (weak, nonatomic) IBOutlet UIView *naView;
@property (weak, nonatomic) IBOutlet UILabel *naTItlelabel;

@property (nonatomic, strong) XWInteractiveTransition *interactivePush;
@property (nonatomic, copy) NSArray *listArray;
@end

@implementation SMHomeViewController


- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if(self) {
        [self orginCof];
    }
    return self;
}

- (void)orginCof {
    self.menuHeight = SBScaleNumber(40);
    self.menuItemWidth = SBScaleNumber(120);
    self.menuViewStyle = WMMenuViewStyleLine;
    self.menuBGColor = [UIColor whiteColor];
    self.listArray = [SMMoneyModel findAll];
    if (self.listArray.count == 0 || self.listArray == nil) {
        SMMoneyModel *mode = [[SMMoneyModel alloc] init];
        mode.saveMoneyTitle = @"无存钱项目";
        self.listArray = @[mode];
    }
    
    NSMutableArray *array = [[NSMutableArray alloc] init];
    for (SMMoneyModel *model in _listArray) {
        [array addObject:model.saveMoneyTitle];
    }
    self.titles = array;
}
 

#pragma mark 返回index对应的标题
- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
    SMMoneyModel *model = _listArray[index];
    return model.saveMoneyTitle;
}

#pragma mark 返回子页面的个数
- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    return _listArray.count;
}

#pragma mark 返回某个index对应的页面，该页面从Storyboard中获取
- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
    SMMoneyModel *model = _listArray[index];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"SMHome" bundle:nil];
    if ([model.saveMoneyTitle isEqualToString:@"无存钱项目"]) {
        SMNULLProjectViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"SMNULLProjectViewController"];
        return controller;
    } else {
        SMMoneyHomeViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"SMMoneyHomeViewController"]; 
        controller.moneyModel = model;
        return controller;
    }
}


- (void)pageController:(WMPageController *)pageController didEnterViewController:(__kindof UIViewController *)viewController withInfo:(NSDictionary *)info {
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.naView.size = CGSizeMake(SBScaleNumber(156), SBScaleNumber(51));
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    UIImageView *navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    navBarHairlineImageView.hidden = YES;
    
    [self orginCof];
    [self reloadData];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    UIImageView *navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    navBarHairlineImageView.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"每天存钱";
}

- (IBAction)leftAction:(id)sender {
    
    
    SMLeftViewController *vc = [[UIStoryboard storyboardWithName:@"Left" bundle:nil] instantiateViewControllerWithIdentifier:@"SMLeftViewController"];
    vc.temVC = self;
    MSNavigationViewController *navc = [[MSNavigationViewController alloc] initWithRootViewController:vc];
    [self presentViewController:navc animated:YES completion:nil];
    
}

- (IBAction)addSaveMoney:(id)sender {
    SMAddSaveMoneyTableViewController *vc = [[UIStoryboard storyboardWithName:@"SMHome" bundle:nil] instantiateViewControllerWithIdentifier:@"SMAddSaveMoneyTableViewController"];
    vc.delegate = self;
    [self presentViewController:vc animated:YES completion:nil];
    
}

- (void)presentedOneControllerPressedDissmiss{
    __weak typeof(self) weakSelf = self;
    [self dismissViewControllerAnimated:YES completion:^{
        [weakSelf orginCof];
        [weakSelf reloadData];
    }];
}

- (id<UIViewControllerInteractiveTransitioning>)interactiveTransitionForPresent{
    return _interactivePush;
}
 

@end
